package control;

import view.Main;
import model.Cargo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author Maja Gliese
 */
public class CargoHandler {

    private SQLCommunication sqlc;
    private ArrayList<Cargo> cargoList;
    private final static String TABEL = "Cargo";
    private Statement stmt;

    public CargoHandler(Main main) {
        this.sqlc = main.getSql();  
        cargoList = new ArrayList<>();  
    }

    public Cargo getCargoByID(int ID) {
        Cargo cargo = null;
        String cargoType;
        int price;
        int lower;
        int upper;
        int cargoWeight;

        try {
            String sql = "select * from " + TABEL + " where cargoID = " + ID;
            ResultSet rs = sqlc.select(TABEL, sql);
            if (rs.next()) {
                ID = rs.getInt("ID");
                cargoType = rs.getString("CargoType");
                price = rs.getInt("Price");
                lower = rs.getInt("Lower");
                upper = rs.getInt("Upper");
                cargoWeight = rs.getInt("CargoWeight");
                cargo = new Cargo(ID, cargoType, price, lower, upper, cargoWeight);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CargoHandler.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Fandt ingen fragt med cargoID = " + ID);
        }
        return cargo;

    }

   


    public Cargo getCargo(String cargoType, int cargoWeight, int cargoNumber) {
        Cargo cargo = null;
        int ID;
        int price;
        int lower;
        int upper;

        try {
            String sql = "select * from " + TABEL + " where cargoType = '" + cargoType + "' and cargoweight >= " + cargoWeight + " and upper >= " + cargoNumber + " order by price;";
            ResultSet rs = sqlc.select(TABEL, sql);
            if (rs.next()) {
                ID = rs.getInt("ID");
                cargoType = rs.getString("CargoType");
                price = rs.getInt("Price");
                lower = rs.getInt("Lower");
                upper = rs.getInt("Upper");
                cargoWeight = rs.getInt("CargoWeight");
                cargo = new Cargo(ID, cargoType, price, lower, upper, cargoWeight);


            }
        } catch (SQLException ex) {
            Logger.getLogger(CargoHandler.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Fandt ingen fragt med cargo = " + cargoType + ", Weight >= " + cargoWeight + " og upper >= " + cargoNumber);
        }
        return cargo;
    }
}